package com.baymax.springboot.cli.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 把普通pojo实例化到spring容器中，相当于配置文件中的
 *
 * 虽然有了@Autowired,但是我们还是要写一堆bean的配置文件,相当麻烦,
 * 而@Component就是告诉spring,我是pojo类,把我注册到容器中吧,spring会自动提取相关信息。
 * 那么我们就不用写麻烦的xml配置文件了
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 步骤1
     * 进入controller层之前拦截请求
     *
     * @param request
     * @param response
     * @param obj
     * @return
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj){
        System.out.println("getContextPath:" + request.getContextPath());
        System.out.println("getServletPath:" + request.getServletPath());
        System.out.println("getRequestURI:" + request.getRequestURI());
        System.out.println("getRequestURL:" + request.getRequestURL());
        System.out.println("getRealPath:" + request.getSession().getServletContext().getRealPath("image"));
        return true;
    }

    /**
     * 步骤2
     * 处理请求完成后视图渲染之前的处理操作
     *
     * @param arg0
     * @param arg1
     * @param arg2
     * @param arg3
     */
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) {

    }

    /**
     * 步骤3
     * 视图渲染之后的操作
     *
     * @param arg0
     * @param arg1
     * @param arg2
     * @param arg3
     */
    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) {

    }
}
